package com.miao.service.jobhander;

import com.miao.FeignClient.EsClient;
import com.miao.FeignClient.po.CourseIndex;
import com.miao.base.exception.XueChengPlusException;
import com.miao.mapper.CourseBaseMapper;
import com.miao.mapper.CoursePublishMapper;
import com.miao.po.MqMessage;
import com.miao.pojo.CourseBase;
import com.miao.pojo.CoursePublish;
import com.miao.service.ICoursePublishService;
import com.miao.service.MessageProcessAbstract;
import com.miao.service.MqMessageService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;

@Slf4j
@Component
public class CoursePublishTask extends MessageProcessAbstract {
    @Autowired
    private ICoursePublishService coursePublishService;
    @Autowired
    private EsClient esClient;
    @Autowired
    private CoursePublishMapper coursePublishMapper;
    @Autowired
    private CourseBaseMapper courseBaseMapper;

    //任务调度入口
    @XxlJob("coursePublishHander")
    public void coursePublishHander() throws Exception{
        // 分片参数
        int shardIndex = XxlJobHelper.getShardIndex();//执行器序号
        int shardTotal = XxlJobHelper.getShardTotal();//执行器总数
        //执行任务
        process(shardIndex, shardTotal, "course_publish", 2);
    }

    //执行发布任务的逻辑
    @Override
    public boolean execute(MqMessage mqMessage) throws Exception {//key为参数，state为各个阶段是否完成的状态
        //拿到课程id
        Long courseId = Long.valueOf(mqMessage.getBusinessKey1());
        //页面静态化
        generateCoursehtml(mqMessage, courseId);
        //es
        saveIndex(mqMessage, courseId);
        //redis

        //状态更改为已发布
        CourseBase courseBase = new CourseBase();
        courseBase.setId(courseId);
        courseBase.setStatus("203002");
        courseBaseMapper.updateById(courseBase);




        return false;
    }

    //静态化页面
    private void generateCoursehtml(MqMessage mqMessage, Long courseId) throws Exception{
        //判断幂等性，看是否已经完成
        Long id = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        int stageOne = mqMessageService.getStageOne(id);
        if(stageOne > 0){
            //完成了
            log.info("课程静态化");
            return;
        }
        //进行课程静态化
        File file = coursePublishService.geneteHtml(courseId);
        if(file == null){
            XueChengPlusException.cast("生成静态页面发生错误！");
        }
        //生成html页面
        coursePublishService.uploadHtml(courseId, file);
        //任务完成，进行标记
        mqMessageService.completedStageOne(id);
    }

    //ES保存
    private void saveIndex(MqMessage mqMessage, Long courseId){
        //判断幂等性，看是否已经完成
        Long id = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        int stageTwo = mqMessageService.getStageTwo(id);
        if(stageTwo > 0){
            //完成了
            log.info("课程静态化");
            return;
        }
        //进行ES同步
        //查询课程
        CoursePublish coursePublish = coursePublishMapper.selectById(courseId);
        //远程调用
        CourseIndex courseIndex = new CourseIndex();
        BeanUtils.copyProperties(coursePublish, courseIndex);
        Boolean add = esClient.add(courseIndex);
        if(!add){
            log.info("es同步失败");
            XueChengPlusException.cast("es同步失败");
        }
        //任务完成，进行标记
        mqMessageService.completedStageTwo(id);
    }

    private void saveRedis(MqMessage mqMessage, Long courseId){
        //判断幂等性，看是否已经完成
        Long id = mqMessage.getId();
        MqMessageService mqMessageService = this.getMqMessageService();
        int stageThree = mqMessageService.getStageThree(id);
        if(stageThree > 0){
            //完成了
            log.info("课程静态化");
            return;
        }
        //进行课程静态化




        //任务完成，进行标记
        mqMessageService.completedStageThree(id);
    }
}
